home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: artemis.sto.fdata.se!news
- From: Niklas Mellin <niklas.mellin@sto.fdata.se>
- Subject: Re: Function Prototypes in C++
- Sender: news@artemis.sto.fdata.se (UseNet NetNews)
- Message-ID: <3163C668.2635@sto.fdata.se>
- Date: Thu, 4 Apr 1996 12:54:00 GMT
- Content-Transfer-Encoding: 7bit
- Content-Type: text/plain; charset=us-ascii
- References: <4junnq$2k1@phoenix.csc.calpoly.edu>
- Mime-Version: 1.0
- X-Mailer: Mozilla 2.0 (WinNT; I)
- Organization: WM-data F÷rsvarsdata AB, Sweden
-
- Nicholas R Bonfilio wrote:
- >
- > I know that function prototypes are required in standard C++ as opposed to C
- > where they are not required. But, what I don't know for certain is the rules
- > regarding the scoping of function protos...
-
- Actually function prototypes are not required if the definition of the function
- is seen before the first call to that function. The main-function is one example
- of a function that is normally not prototyped.
-
- > That is, can I declare the prototypes as "global": external to the main()
- > routine?
-
- Yes.
-
- > Should all prototypes go inside of the main() routine?
-
- Normally no.
-
- > Or should the prototypes be placed inside of each routine where the
- > particular function is called? (This could have been asked in the C lang
- > discussion group, I realize.)
-
- If you prototype a function inside another function the prototype is only
- visible within that functions scope.
-
- Normally prototypes for functions that are called from more than one
- translation unit are put in header files that can be included to save
- typing and typeing errors.
-
- [Example substituted with another one]
-
- #include <math.h> // global function prototypes for sin, cos etc.
-
- void f(); // global prototype
-
- int main()
- {
- void g(); // local prototype
- g(); // ok, prototyped above
- h(); // error, no prototype for h
- sin(0.2); // ok, global prototyp included in math.h
- return 0;
- }
-
- void h()
- {
- f(); // ok, f prototyped globally above
- g(); // error, prototype in main not visible here
- }
-
- void hh()
- {
- h(); // ok, definition of h seen above
- }
-
- void g()
- {
- }
-
- ---
- Niklas Mellin
-